//Processing Controller
hfrApp.registerCtrl("AddNewWordpressController", function ($scope, $http, $log, $window, $rootScope, HfrConfig, HfrStorage, HfrInit) {	
	
	//Init message alert
	HfrInit.message();
	
	//Display meta header
	HfrConfig.displayMeta("addNewWordpress");
	
	$scope.check_info = function (form) {
		// Trigger validation flag && check valid form
	    $scope.submitted = true;
	      
		if (form.$invalid) {
			console.log(form);
	        return;
	    }
		
		var checkUrl = HfrConfig.ApiServiceUrl.checkPostUrlApi + "?username=" + $scope.username
				+ "&password="+$scope.password
				+ "&type=Wordpress"
				+"&url=" + encodeURIComponent($scope.siteurl);
		
		$http.get(checkUrl).success(function (data) {
			if (data.status == 200) {
				$scope.success = true;
				$rootScope.message.success = true;
				$rootScope.message.text = "We has post success test data.";
				$scope.next = true;
			} else {
				$scope.success = false;
				$rootScope.message.error = true;
				$rootScope.message.text = "Change a few things up and try submitting again.";
				$scope.next = false;
			}
			
		})
		.error(function(data) {
	    	$scope.error = true;
	    	$rootScope.message.error = true;
	    	$rootScope.message.text = "Can't connection to server";
	    });
		
		return false;
	};
	
	
	$scope.create_site = function (form) {
		
		//Proccsing data
		$rootScope.siteurl = $scope.siteurl;
		
		$scope.data = {
			blog:{
				url: $scope.siteurl,
				customerId: $rootScope.username,
				type: "Wordpress",
				username: $scope.username,
				password: $scope.password,
			}	
		};
		
		console.log($scope.data);
		
		$http.post(HfrConfig.ApiServiceUrl.blogsUrlApi, $scope.data).success(function (data) {
			if (data.status == 200) {
				HfrStorage.set("siteUrl", $scope.siteurl);
				$window.location.href= "#/add-topics";
			} else {
				$scope.error = true;
				$rootScope.message.error = true;
		    	$rootScope.message.text = data.message;
			}
		})
	    .error(function(data) {
	    	$scope.error = true;
	    	$rootScope.message.error = true;
	    	$rootScope.message.text = "Can't post data to server";
	    	
	    });
		
		return false;
	}
});